var context;
var currentState;
var buffer = new Array();

var ZONE_JEU_WIDTH = 1024;
var ZONE_JEU_HEIGHT = 768;
var deplacement = 35;
var timing = 20;

var hauteurDunPouer = 150;
var largeurDunPouer = 120;

var BeerSpeed = 17;

var openTime = 10;
var openBound = 20;

var posXPouer;
var posYPouer;

var posXMouer;
var posYMouer;

var p1;
var p2;

var hitP1;
var hitP2;

var pouerBeers = new Array();
var mouerBeers = new Array();

var scoreP1;
var scoreP2;

var shootingP1 = 0;
var shootingP2 = 0;

var pouersos = new Array();
pouersos.push(new Array("julien","Maaaaaange mes buuuurnes !"));
pouersos.push(new Array("baptiste","Ma BITE !"));
pouersos.push(new Array("kevin","Nooon les gaars, putain NOOON"));
pouersos.push(new Array("geoffrey","Les vaches les vaches ouais les chevres."));
pouersos.push(new Array("clement","Battlefield mec, DIABLO ouais starcraft !"));
pouersos.push(new Array("vivien","LoL mec!"));

var bieres = new Array();
bieres.push("kro");
bieres.push("maredsous");
bieres.push("desperados");
bieres.push("topbudget");

var sounds = new Array();
sounds.push("explosion.mp3");
sounds.push("hit.wav");

var yvans = new Array();
yvans.push("fond.png");
yvans.push("fond2.png");
yvans.push("fond3.png");


var mots = new Array();

$(function()
{
	// Playeeeeeeers
	p1 = pouersos[Math.floor(Math.random()*pouersos.length)];
	p2 = pouersos[Math.floor(Math.random()*pouersos.length)];
	while(p1 == p2)
		p2 = pouersos[Math.floor(Math.random()*pouersos.length)];

	var fond  = yvans[Math.floor(Math.random()*yvans.length)];
	var url = "url("+fond+")";
	
	// Get canvas
	buffer[0] = document.getElementById('gameContext1');
	$('#gameContext1').css("background-image", url); 
	if (!buffer[0] || !buffer[0].getContext) {
		return;
	}
	buffer[1] = document.getElementById('gameContext2');
	$('#gameContext2').css("background-image", url); 
	if (!buffer[1]|| !buffer[1].getContext) {
		return;
	}
	buffer[0].style.visibility='visible';
	context = buffer[0].getContext('2d');
	if (!context) {
		return;
	}

	currentState = 0;
	// Calcule la pouer-taille
	faireQueLeJeuIlALaBonneTaille();
	// Initialise les poueros
	initPouer();
	// Rafraichi les poueros
	idInterv = setInterval(refreshPouer,timing);
	
	// Touche clavier
	$(window).keydown(checkPouer);
});

function afficherLesControles()
{
	var controles = "Contrôles : a q w z s x o l ; i k ,";
	context.font = "bold 20px Calibri,Geneva,Arial";
	context.fillStyle = "black";
	context.fillRect(40,20,320,50);
	context.fillStyle = "red";
	context.fillText(controles, 50, 50);
}

function initPouer()
{
	posXPouer = largeurDunPouer;
	posYPouer = ZONE_JEU_HEIGHT/2;

	posXMouer = ZONE_JEU_WIDTH-largeurDunPouer;
	posYMouer = ZONE_JEU_HEIGHT/2;

	scoreP1 = 0;
	hitP1 = 0;
	scoreP2 = 0;
	hitP2 = 0;
}

function refreshPouer()
{
	context = buffer[currentState].getContext('2d');
	flip();
	clearContexte(context, 0, ZONE_JEU_WIDTH, 0, ZONE_JEU_HEIGHT);
	afficherLesControles();	
	dessineLesMots();
	if(shootingP1 > 0)
		shootingP1--;
	if(shootingP2 > 0)
		shootingP2--;

	addPouer(1);
	addPouer(42);
	addBeers();
	addScore();
		
}

function faireQueLeJeuIlALaBonneTaille()
{
	ZONE_JEU_WIDTH = window.innerWidth;
	ZONE_JEU_HEIGHT = window.innerHeight;
	buffer[0].getContext('2d').canvas.width  = ZONE_JEU_WIDTH;
	buffer[0].getContext('2d').canvas.height  = ZONE_JEU_HEIGHT;
	buffer[1].getContext('2d').canvas.width = ZONE_JEU_WIDTH;
	buffer[1].getContext('2d').canvas.height = ZONE_JEU_HEIGHT;
}

function addScore()
{
	context.font = "50px Calibri,Geneva,Arial";
	context.fillStyle = "red";
	context.fillText(scoreP1, ZONE_JEU_WIDTH/2 - 100, 50);
	context.fillText(scoreP2, ZONE_JEU_WIDTH/2 + 100, 50);
}

function dessineLesMots()
{

	context.font = "20px Calibri,Geneva,Arial";
	context.fillStyle = "#fdb2b2";
	
	for (i=0; i<mots.length; i++)
	{
		mot = mots[i];
		context.fillText(mot[0], mot[1], mot[2]);
	}
}

function addPouer(who){
	if (who == 1)
	{
		if(shootingP1 <= 0)
			dessineUnTruc(p1[0],posXPouer,posYPouer)
		else
			dessineUnTruc(p1[0]+"ouvre",posXPouer,posYPouer)
		if(hitP1 > 0) {
			dessineUnTruc("explosion",posXPouer,posYPouer);
			hitP1--;
		}
		
	}
	else
	{
		if(shootingP2 <= 0)
			dessineUnTruc(p2[0]+"g",posXMouer,posYMouer)
		else
			dessineUnTruc(p2[0]+"gouvre",posXMouer,posYMouer)
		if(hitP2 > 0) {
			dessineUnTruc("explosion",posXMouer,posYMouer);
			hitP2--;
		}
	}
}



function moveMouer(who, whereX, whereY)
{
	if (who == 1){
		posXPouer += whereX;
		posYPouer += whereY;	
	} else {
		posXMouer += whereX;
		posYMouer += whereY;	
	}
}

function clearContexte(ctx, startwidth, ctxwidth, startheight, ctxheight)
{
	ctx.clearRect(startwidth, startheight, ctxwidth, ctxheight);
}

function checkPouer(e)
{
	switch (e.keyCode)
	{
	// POUER UN
	case 65: // A
		if(posYPouer > 0) {
			posYPouer -= deplacement;
		}
		break;
	case 81: // Q
		if(posXPouer > 0) {
			posXPouer -= deplacement;
		}
		break;
	case 87: // W
		if(posYPouer < ZONE_JEU_HEIGHT) {
			posYPouer += deplacement;
		}
		break;
	case 83: // S
		if(posXPouer < ZONE_JEU_WIDTH) {
			posXPouer += deplacement;
		}
		break;
	case 90: // Z
		launchBeer(1, posXPouer, posYPouer);
		if(shootingP1 < openBound)
			shootingP1 += openTime;
		break;
	case 88: // X premier pouer gueule
		gueule(1);
		break;

	
	// POUER DEUX
	case 75: // K
		if(posXMouer > 0) {
			posXMouer -= deplacement;
		}
		break;
	case 79: // O
		if(posYMouer > 0) {
			posYMouer -= deplacement;
		}
		break;
	case 76: // L
		if(posXMouer < ZONE_JEU_WIDTH) {
			posXMouer += deplacement;
		}
		break;
	case 186: // ;
		if(posYMouer < ZONE_JEU_HEIGHT) {
			posYMouer += deplacement;
		}
		break;
	case 188: // ,
		gueule(2);
		break;
	case 73: // I
		launchBeer(42, posXMouer, posYMouer);
		if(shootingP2 < openBound)
			shootingP2 += openTime;
		break;
	 }
	
}

function gueule(who)
{
	context.font = "20px Calibri,Geneva,Arial";
	if (who == 1)
	{
		x = posXPouer+largeurDunPouer/2 + 10;
		y = posYPouer+((hauteurDunPouer/12)*5);
		mot = new Array(p1[1],x,y);
		mots.push(mot);
	}
	else
	{
		x = posXMouer - largeurDunPouer/2 - context.measureText(p2[1]).width - 10;
		y = posYMouer+((hauteurDunPouer/12)*5);
		mot = new Array(p2[1],x,y);
		mots.push(mot);
	}
}

function dessineUnTruc(leNom,leX,leY,leAngle)
{
	path = "ingame/"+leNom+".png";
	var img = new Image();
	img.src = path;
	img.onload = function(){ context.save(); context.translate(leX, leY); context.rotate(leAngle); context.drawImage(img, -(img.width/2), -(img.height/2)); context.restore(); }
}

function faisDuBruit(leBruit)
{
	var emplacement = "ingame/"+leBruit;
	var snd = new Audio(emplacement); // buffers automatically when created
	snd.play();
}

function launchBeer(who, x, y)
{
	if(who ==1){
		if(pouerBeers.length < 10)
			pouerBeers.push(new Array(bieres[Math.floor(Math.random()*bieres.length)],x+largeurDunPouer/2,y+(hauteurDunPouer/12 * 5),0.0));
	}
	else{
		if(mouerBeers.length < 10)
			mouerBeers.push(new Array(bieres[Math.floor(Math.random()*bieres.length)],x-largeurDunPouer/2,y+(hauteurDunPouer/12 * 5),0.0));
	}
}

function addBeers()
{
	for (i=0; i<pouerBeers.length; i++)
	{
		if(pouerBeers[i][1] < ZONE_JEU_WIDTH +50) {
			pouerBeers[i][1] += BeerSpeed;
			pouerBeers[i][3] += 0.10;
			checkHit(1,i);
			dessineUnTruc(pouerBeers[i][0],pouerBeers[i][1],pouerBeers[i][2]-25,pouerBeers[i][3]);
		}
		else {
			pouerBeers.splice(i, 1);
		}
	}

	for (i=0; i<mouerBeers.length; i++)
	{
		if(mouerBeers[i][1] > 0-50) {
			mouerBeers[i][1] -= BeerSpeed;
			mouerBeers[i][3] += 0.30;
			checkHit(42,i);
			dessineUnTruc(mouerBeers[i][0],mouerBeers[i][1],mouerBeers[i][2]-25,mouerBeers[i][3]);
		}
		else {
			mouerBeers.splice(i, 1);
		}
	}
	
}

function checkHit(who,what)
{
	if(who == 1) {
	
		if(pouerBeers[what][1] >= posXMouer-largeurDunPouer/2 - 25 && pouerBeers[what][1] <= posXMouer+largeurDunPouer/2 + 25) {
			if(pouerBeers[what][2] >= posYMouer-hauteurDunPouer/2 && pouerBeers[what][2] <= posYMouer + hauteurDunPouer/2 + 50) {
				pouerBeers.splice(i, 1);
				hitP2 = 20;
				scoreP1--;
				faisDuBruit(sounds[Math.floor(Math.random()*pouersos.length)]);	
			}
		}
	} else {
		if(mouerBeers[what][1] >= posXPouer-largeurDunPouer/2 - 25 && mouerBeers[what][1] <= posXPouer+largeurDunPouer/2 + 25) {
			if(mouerBeers[what][2] >= posYPouer-hauteurDunPouer/2 && mouerBeers[what][2] <= posYPouer + hauteurDunPouer/2 + 50) {
				mouerBeers.splice(i, 1);
				hitP1 = 20;
				scoreP2--;
				faisDuBruit(sounds[Math.floor(Math.random()*pouersos.length)]);
			}
		}
	}
}

function flip()
{
	buffer[currentState].style.visibility='hidden';
	currentState = (currentState == 0) ? 1 : 0;
	buffer[currentState].style.visibility='visible';
}

